package com.young.weixin.rpc.impl;

import com.young.common.util.HttpUtil;
import com.young.common.util.JsonUtil;
import com.young.common.util.StringUtils;
import com.young.interfaces.log.annotation.Log;
import com.young.weixin.configure.WxConfigure;
import com.young.weixin.model.wxrpc.response.GetAccessTokenResponse;
import com.young.weixin.rpc.IWxBaseRpc;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 微信基础rpc调用
 *
 * @author wangjiyu
 * @date 2021/10/5 13:12
 */
@Service
public class WxBaseRpcImpl implements IWxBaseRpc {

    private static final Logger logger = LoggerFactory.getLogger(WxBaseRpcImpl.class);

    /**
     * 微信配置
     */
    @Autowired
    private WxConfigure wxConfigure;


    /**
     * 查询access_token
     *
     * @return access_token查询结果
     */
    @Log("微信rpc-查询access_token")
    @Override
    public GetAccessTokenResponse getAccessToken() {
        String url = wxConfigure.getAccessTokenUrl();
        logger.info("[查询access_token] url={}", url);
        String res = HttpUtil.get(url);
        logger.info("[查询access_token] 微信返回结果={}", res);
        if (StringUtils.isNotBlank(res)) {
            return JsonUtil.toBean(res, GetAccessTokenResponse.class);
        }
        return null;
    }
}
